package clustering;
import java.io.*;
import java.util.*;
import preprocess.*;
public class SeedClustering {
	private String rawfile = ""; // xml file
	private String filtered_positive_pairs =""; 
	private String filtered_base_pairs ="";
	private String seedcluster_sim_file = ""; // output file
	public static void main(String args[]){
		new SeedClustering().Procedure();
	}
	public void Procedure(){
		List <ArticleUnit> articleunits = new One_Instance_Filter().XMLParser(rawfile);
		SeedCluster_Identity seedcluster = new SeedCluster_Identity(filtered_positive_pairs, filtered_base_pairs);
		List<Article> articles = BuildArticlesforClusterUsage(articleunits, seedcluster);
		GenerateSeedCluster_SimilarityFile(seedcluster_sim_file, articles, seedcluster);
		/*The seedcluster_sim_file is the input for average_link.py code*/
		
	}
	public List<Article> BuildArticlesforClusterUsage(List<ArticleUnit> article_units, SeedCluster_Identity seedcluster){
		List<Article> articles = new ArrayList<Article>();
		Text_Clean cleanser = new Text_Clean();
		for(ArticleUnit au: article_units){
			Article a = new Article();
			a.setPmid(au.getPmid());
			List<String> cold_locals = seedcluster.GetLocalforCold(au.getCold_Sentence());	
			a.setCold_bigrams(cold_locals, cleanser);
			a.setCold_trigrams(cold_locals, cleanser);
			a.setTiab(au.getTitle()+"\t"+au.getAbstract_info());
			articles.add(a);
		}
		return articles;
	}
	
	public void GenerateSeedCluster_SimilarityFile(String writefile, List<Article> articles, SeedCluster_Identity seedcluster){
		File f = new File(writefile);
		OutputStreamWriter write;
		try{
			write = new OutputStreamWriter(new FileOutputStream(f,false));
			BufferedWriter bw =new BufferedWriter(write);
			for(int i=0; i<articles.size(); i++)
				for(int j=i+1; j<articles.size(); j++){
					int score = seedcluster.Get_Score_from_Seeds(true, true, true, articles.get(i), articles.get(j));
					bw.write(String.valueOf(i)+" "+String.valueOf(j)+" "+String.valueOf(score));
					bw.newLine();
				}
			bw.close();
		}catch(IOException e){
			e.printStackTrace();
		}
	}

}
